/*
Copyright (c) 2011 - 2016 Qualcomm Technologies International, Ltd.
*/

/*!
@file
@ingroup sink_app

*/


/****************************************************************************
    Header files
*/

#include "source_device_id.h"
#include "source_a2dp.h"
#include <ps.h>
#include <panic.h>
#include <sdp_parse.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <bdaddr.h>
#include <sdp_parse.h>
#include <connection.h>

#ifdef DEBUG_DI
    #define DI_DEBUG(x) DEBUG(x)
#else
    #define DI_DEBUG(x)
#endif

#define VID_APPLE         0x4C
#define PID_AIRPODS_PRO2  0x2014
#define VENDOR_ID_SOURCE_SIG  0x01

/* Prototype and task structure for Peer Device credentials specific SDP searches */
static void HandleSdpMessage (Task task, MessageId id, Message message);
static const struct TaskData deviceIdTask = { HandleSdpMessage };

/* DataElSeq(0x35), Length(0x03), 16-bit UUID(0x19), PnPInformation(0x1200) */
static const uint8 device_id_search_pattern[] = {0x35, 0x03, 0x19, 0x12, 0x00};
/* DataElSeq(0x35), Length(0x09), DataElUint16(0x09), VendorIdSource(0x0205), DataElUint16(0x09), VendorID(0x0201), DataElUint16(0x09), ProductID(0x0202) */
static const uint8 device_id_attr_list[] = {0x35, 0x09, 0x09, 0x02, 0x01, 0x09, 0x02, 0x02, 0x09, 0x02, 0x05};

bool SourceRequestRemoteDeviceId (const bdaddr *bd_addr)
{
    ConnectionSdpServiceSearchAttributeRequest((Task)&deviceIdTask, bd_addr, 32, sizeof(device_id_search_pattern), device_id_search_pattern, sizeof(device_id_attr_list), device_id_attr_list);

    return TRUE;
}

static bool CheckRemoteDeviceId (const uint8 *attr_data, uint8 attr_data_size)
{
    uint32 vendorId, productId, vendorIdSrc;

    if ( SdpParseGetArbitrary(attr_data_size, attr_data, 0x0201, &vendorId) &&
         SdpParseGetArbitrary(attr_data_size, attr_data, 0x0202, &productId) &&
         SdpParseGetArbitrary(attr_data_size, attr_data, 0x0205, &vendorIdSrc) )
    {
        if((vendorId == VID_APPLE) && (productId == PID_AIRPODS_PRO2) && (vendorIdSrc == VENDOR_ID_SOURCE_SIG))
        {
            return TRUE;
        }
    }
    
    return FALSE;
}

/*************************************************************************
NAME
    handleCredentialsSdpServiceSearchAttributeCfm

DESCRIPTION
    Handles search results returned from requesting Peer credentials (Device Id)

RETURNS
    None

**************************************************************************/
static void handleSdpServiceSearchAttributeCfm (const CL_SDP_SERVICE_SEARCH_ATTRIBUTE_CFM_T *cfm)
{
    if((cfm->status==sdp_response_success) && (!cfm->error_code))
    {   /* Response obtained, examine it */
        if(CheckRemoteDeviceId(cfm->attributes, (uint8)cfm->size_attributes))
        {
            /* Remote is apple airpods pro2 */
            a2dpInstance *inst = a2dp_get_instance_from_bdaddr(&cfm->bd_addr);

            if(inst)
            {
                inst->apple_airpods_pro2 = TRUE;
            }
        }
    }
}

/****************************************************************************
NAME
    handleCredentialsMessage

DESCRIPTION
    Task function to handle responses to Peer device specific credentials requests

RETURNS
    void
*/
static void HandleSdpMessage (Task task, MessageId id, Message message)
{
    UNUSED(task);

    switch (id)
    {
    case CL_SDP_SERVICE_SEARCH_ATTRIBUTE_CFM:
        DI_DEBUG(("CL_SDP_SERVICE_SEARCH_ATTRIBUTE_CFM\n"));
        handleSdpServiceSearchAttributeCfm ((const CL_SDP_SERVICE_SEARCH_ATTRIBUTE_CFM_T*) message);
        break;

    default:
        DI_DEBUG(("Unexpected message, id=0x%X\n", id));
        break;
    }
}
